class Shop::SkuImagesController < ShopController
  before_action :set_sku

  def index
    @sku_images = @sku.sku_images.order("sort ASC")
  end

  def create
    @sku_image = @sku.sku_images.build sku_image_params
    if @sku_image.save
      redirect_to shop_sku_sku_images_path(@sku), notice: "保存成功！"
    else
      redirect_to shop_sku_sku_images_path(@sku), notice: "保存失败！"
    end
  end
  
  def destroy
    @sku_image = SkuImage.find params[:id]
    if @sku_image.destroy
      redirect_to shop_sku_sku_images_path(@sku), notice: "删除成功."
    else
      redirect_to shop_sku_sku_images_path(@sku), notice: "删除失败."
    end
  end

  private

  def set_sku
    @sku = Sku.find params[:sku_id]
  end

  def sku_image_params
    params.require(:sku_image).permit(:image, :sort, :sku_id)
  end
end
